package com.yungu.swift.assets.passenger.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.assets.passenger.dao.PassengerEmergencyMapper;
import com.yungu.swift.assets.passenger.model.dto.PassengerEmergencyDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerEmergencyMemberDto;
import com.yungu.swift.assets.passenger.model.vo.PassengerEmergencyVo;
import com.yungu.swift.assets.passenger.service.PassengerEmergencyMemberService;
import com.yungu.swift.assets.passenger.service.PassengerEmergencyService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 乘客紧急方式表
 *
 * @author Zerrion
 * @version 1.0.3-RELEASE
 * @since 2018-10-16 10:35:12
 */

@Service
public class PassengerEmergencyServiceImpl extends BaseServiceImpl<PassengerEmergencyDto> implements PassengerEmergencyService {

    @Autowired
    private PassengerEmergencyMapper passengerEmergencyMapper;
    @Autowired
    private PassengerEmergencyMemberService passengerEmergencyMemberService;

    @Override
    protected IMapper<PassengerEmergencyDto> getMapper() {
        return passengerEmergencyMapper;
    }


    @Override
    public ResponseData<String> addMember(String passengerUuid, String memberName, String memberMobile) {
        ResponseData<List<PassengerEmergencyMemberDto>> checkListMember = passengerEmergencyMemberService.list(
                MapUtils.build(new String[]{"passengerUuid", "memberName", "memberMobile"},
                        new Object[]{passengerUuid, memberName, memberMobile}));
        if (checkListMember.isSuccess() && CollectionUtils.isNotEmpty(checkListMember.getData())) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "已存在该紧急联系人");
        }
        Date nowDate = new Date();
        String emergencyUuid;
        List<PassengerEmergencyDto> listEmergencyDto = passengerEmergencyMapper.list(MapUtils.build("passengerUuid", passengerUuid));
        if (null == listEmergencyDto || listEmergencyDto.size() == 0) {
            PassengerEmergencyDto addEmergencyDto = new PassengerEmergencyDto();
            addEmergencyDto.setUuid(StringUtils.buildUUID());
            addEmergencyDto.setPassengerUuid(passengerUuid);
            addEmergencyDto.setEmergencyStatus(PassengerEmergencyDto.EMERGENCY_STATUS_VALID);
            addEmergencyDto.setCreateOn(nowDate);
            passengerEmergencyMapper.add(addEmergencyDto);
            emergencyUuid = addEmergencyDto.getUuid();
        } else {
            emergencyUuid = listEmergencyDto.get(0).getUuid();
        }
        ResponseData<List<PassengerEmergencyMemberDto>> listMember = passengerEmergencyMemberService.list(
                MapUtils.build(new String[]{"passengerUuid"},
                        new Object[]{passengerUuid}));
        PassengerEmergencyMemberDto addEmergencyMemberDto = new PassengerEmergencyMemberDto();
        addEmergencyMemberDto.setUuid(StringUtils.buildUUID());
        addEmergencyMemberDto.setPassengerUuid(passengerUuid);
        addEmergencyMemberDto.setEmergencyUuid(emergencyUuid);
        addEmergencyMemberDto.setMemberName(memberName);
        addEmergencyMemberDto.setMemberMobile(memberMobile);
        addEmergencyMemberDto.setMemberStatus(PassengerEmergencyMemberDto.MEMBER_STATUS_VALID);
        addEmergencyMemberDto.setCreateOn(nowDate);
        if(null == listMember.getData() || listMember.getData().size() == 0) {
            addEmergencyMemberDto.setIsDefault(1);
        }
        if(null == listEmergencyDto || listEmergencyDto.size() == 0) {
            addEmergencyMemberDto.setIsDefault(1);
        }
        passengerEmergencyMemberService.add(addEmergencyMemberDto);
        return ResponseData.buildSuccessResponse("新增紧急联系成员成功", emergencyUuid);
    }


    @Override
    public ResponseData<String> delMember(String emergencyUuid, String memberUuid) {
        PassengerEmergencyMemberDto delEmergencyMemberDto = new PassengerEmergencyMemberDto();
        delEmergencyMemberDto.setUuid(memberUuid);
        passengerEmergencyMemberService.del(delEmergencyMemberDto);
        return ResponseData.buildSuccessResponse("删除紧急联系人成功", emergencyUuid);
    }

    @Override
    public ResponseData<String> updateMember(String emergencyUuid, String memberUuid, String memberName, String memberMobile) {
        PassengerEmergencyMemberDto editEmergencyMemberDto = new PassengerEmergencyMemberDto();
        editEmergencyMemberDto.setUuid(memberUuid);
        editEmergencyMemberDto.setMemberName(memberName);
        editEmergencyMemberDto.setMemberMobile(memberMobile);
        editEmergencyMemberDto.setUpdateOn(new Date());
        passengerEmergencyMemberService.edit(editEmergencyMemberDto);
        return ResponseData.buildSuccessResponse("更新紧急联系人成功", emergencyUuid);
    }

    @Override
    public ResponseData<Integer> updateStatus(String emergencyUuid, Integer emergencyStatus, Date emergencyTimeStart, Date emergencyTimeEnd) {
        PassengerEmergencyDto emergencyDto = passengerEmergencyMapper.list(MapUtils.build("uuid", emergencyUuid)).get(0);
        if (null == emergencyDto) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取不到紧急联系方式");
        }
        //更新成员信息状态
        List<PassengerEmergencyMemberDto> listEmergencyMemberDto = passengerEmergencyMemberService.list(MapUtils.build(
                "emergencyUuid", emergencyUuid)).getData();
        if (CollectionUtils.isNotEmpty(listEmergencyMemberDto)) {
            ResponseData<Boolean> responseData = passengerEmergencyMemberService.patchUpdateStatus(emergencyUuid, emergencyStatus);
            if (!responseData.isSuccess()) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "切换总自动分享行程信息状态失败");
            }
        }
        PassengerEmergencyDto editEmergencyDto = new PassengerEmergencyDto();
        editEmergencyDto.setUuid(emergencyUuid);
        editEmergencyDto.setEmergencyStatus(emergencyStatus);
        editEmergencyDto.setEmergencyTimeStart(emergencyTimeStart);
        editEmergencyDto.setEmergencyTimeEnd(emergencyTimeEnd);
        editEmergencyDto.setUpdateOn(new Date());
        passengerEmergencyMapper.edit(editEmergencyDto);
        return ResponseData.buildSuccessResponse("切换总自动分享行程信息状态成功", emergencyStatus);
    }


    @Override
    public ResponseData<Integer> updateMemberStatus(String emergencyUuid, String memberUuid, Integer memberStatus) {
        PassengerEmergencyMemberDto editEmergencyMemberDto = new PassengerEmergencyMemberDto();
        editEmergencyMemberDto.setUuid(memberUuid);
        editEmergencyMemberDto.setMemberStatus(memberStatus);
        editEmergencyMemberDto.setUpdateOn(new Date());
        passengerEmergencyMemberService.edit(editEmergencyMemberDto);

        //判断设置总自动分享状态
        int memberStatusCount = 0;
        int emergencyStatus = 1;
        List<PassengerEmergencyMemberDto> listEmergencyMemberDto = passengerEmergencyMemberService.list(
                MapUtils.build("emergencyUuid", emergencyUuid)).getData();
        if (null != listEmergencyMemberDto && listEmergencyMemberDto.size() > 0) {
            for (PassengerEmergencyMemberDto memberDto : listEmergencyMemberDto) {
                if (PassengerEmergencyMemberDto.MEMBER_STATUS_INVALID == memberDto.getMemberStatus()) {
                    memberStatusCount++;
                }
            }
            //全部为不分享->设置为0，否则设置为1，暂时采用每次更新都设置方案。20181016
            if (memberStatusCount == listEmergencyMemberDto.size()) {
                emergencyStatus = 0;
            }
            PassengerEmergencyDto editEmergencyDto = new PassengerEmergencyDto();
            editEmergencyDto.setUuid(emergencyUuid);
            editEmergencyDto.setEmergencyStatus(emergencyStatus);
            editEmergencyDto.setUpdateOn(new Date());
            passengerEmergencyMapper.edit(editEmergencyDto);
        }
        return ResponseData.buildSuccessResponse("切换总自动分享行程信息状态成功", emergencyStatus);
    }

    @Override
    public ResponseData<PassengerEmergencyVo> emergencyInfo(String passengerUuid) {
        return ResponseData.buildSuccessResponse(passengerEmergencyMapper.emergencyInfo(MapUtils.build("passengerUuid",
                passengerUuid)));
    }

}
